﻿using System;
using System.Configuration;
using System.Linq;
using Controllers;

namespace Web
{
    public partial class InviteEdit : System.Web.UI.Page
    {
        protected ReqInvite reqInvite;
        protected string deptName;
        protected string empName;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                ApolloOaDataContext ctx = new ApolloOaDataContext(ConfigurationManager.ConnectionStrings["SqlConnectionString"].ConnectionString);
                string formNo = Request.QueryString["formNo"];
                if (formNo != null)
                {
                    reqInvite = ctx.ReqInvites.Single(r => r.FormNo == formNo);
                    Employee employee = ctx.Employees.Single(r => r.Id == reqInvite.EmpId);
                    empName = employee.Name;
                    deptName = ctx.Depts.Single(r => r.Id ==reqInvite.DeptId).Text;
                    Position.Value = reqInvite.Position;
                    ReqNum.Value = reqInvite.ReqNum.ToString();
                    Salary.Value = reqInvite.Salary;
                    OnDutyDate.Value = reqInvite.OnDutyDate.ToString("yyyy-MM-dd");
                    Reason.Value = reqInvite.Reason;
                    PositionDesc.Value = reqInvite.PositionDesc;
                    if(reqInvite.Sex!=null)
                    {
                        Sex.Value = reqInvite.Sex.ToString();
                    }
                    Age.Value = reqInvite.Age;
                    Education.Value = reqInvite.Education;
                    Major.Value = reqInvite.Major;
                    English.Value = reqInvite.English;
                    ProfTitle.Value = reqInvite.ProfTitle;
                    Qualify.Value = reqInvite.Qualify;
                    Experience.Value = reqInvite.Experience;
                    EssenKnowledge.Value = reqInvite.EssenKnowledge;
                    Skill.Value = reqInvite.Skill;
                    Trait.Value = reqInvite.Trait;
                    Remarks.Value = reqInvite.Remarks;
                }
                else
                {
                    EmpBasicInfo ebi = Session["ebi"] as EmpBasicInfo;
                    empName = ebi.Name;
                    deptName = ctx.Depts.Single(r => r.Id == ebi.EmpDepPos.OrderByDescending(r2 => r2.PosId).First().DeptId).Text;
                    reqInvite = new ReqInvite();
                    reqInvite.ReqDate = DateTime.Now;
                }
            }
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            _formSave(0);
        }
        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            _formSave(1);
        }
        private void _formSave(int isSubmit)
        {
            ApolloOaDataContext ctx = new ApolloOaDataContext(ConfigurationManager.ConnectionStrings["SqlConnectionString"].ConnectionString);
            string formNo = Request.QueryString["formNo"];
            EmpBasicInfo ebi = Session["ebi"] as EmpBasicInfo;
            if (!string.IsNullOrEmpty(formNo) && formNo != "undefined")
            {
                reqInvite = ctx.ReqInvites.Single(r => r.FormNo == formNo);
                //删除之前的签核数据
                var list = from r in ctx.SignFlows where r.FormNo == formNo select r;
                ctx.SignFlows.DeleteAllOnSubmit(list);
            }
            else
            {
                reqInvite = new ReqInvite();
                string maxFormNo = ctx.ReqInvites.Max(r => r.FormNo);
                formNo = "Form_Invi_000001";
                if (!string.IsNullOrEmpty(maxFormNo))
                {
                    formNo = "Form_Invi_" + (int.Parse(maxFormNo.Substring(10)) + 1000001).ToString().Substring(1);
                }
                reqInvite.FormNo = formNo;
                reqInvite.ReqDate = DateTime.Now;
                reqInvite.DeptId = ebi.EmpDepPos.OrderByDescending(r2 => r2.PosId).First().DeptId;
                reqInvite.EmpId =ebi.Id;
                ctx.ReqInvites.InsertOnSubmit(reqInvite);
            }
            reqInvite.Position = Position.Value;
            reqInvite.ReqNum = int.Parse(ReqNum.Value);
            reqInvite.Salary = Salary.Value;
            reqInvite.OnDutyDate = DateTime.Parse(OnDutyDate.Value);
            reqInvite.Reason = Reason.Value;
            reqInvite.PositionDesc = PositionDesc.Value;
            if(!string.IsNullOrEmpty(Sex.Value))
            {
                reqInvite.Sex =char.Parse(Sex.Value);
            }
            reqInvite.Age = Age.Value;
            reqInvite.Education = Education.Value;
            reqInvite.Major = Major.Value;
            reqInvite.English = English.Value;
            reqInvite.ProfTitle = ProfTitle.Value;
            reqInvite.Qualify = Qualify.Value;
            reqInvite.Experience = Experience.Value;
            reqInvite.EssenKnowledge = EssenKnowledge.Value;
            reqInvite.Skill = Skill.Value;
            reqInvite.Trait = Trait.Value;
            reqInvite.Remarks = Remarks.Value;
            reqInvite.StatusId = isSubmit;
            ctx.SubmitChanges();
            if (isSubmit == 1)//写签核流程
            {
                FlowSet fs = new FlowSet();
                fs.SetSignFlow(reqInvite.FormNo, ebi);
            }
            Response.Write("<script>alert('修改成功');window.returnValue='" + reqInvite.FormNo + "';window.close();</script>");
        }
    }
}